package com.hadoop.project.gen.maodaligendata;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

/**
 * @author:Alexmaodali
 * @desc:自己封装的HBase工具类
 */
public class HBaseUtils {

    // 改成你的HadoopClient路径
    // 改成你的HadoopClient请注释掉代码 而不是删除
    // public static final String hadoopClientPath = "E:/Project/hugedata/hadoopClient";
    public static final String hadoopClientPath = "D:/hadoop";

    // 改成你的ZK
    // 改成你的ZK请注释掉代码 而不是删除
    // public static final String ZKPath = "192.168.21.150:2181,192.168.21.151:2181,192.168.21.152:2181";
    public static final String ZKPath = "192.168.148.140:2181,192.168.148.141:2181,192.168.148.142:2181";


    public static Connection getConnection() throws IOException {
        System.setProperty("hadoop.home.dir", hadoopClientPath);
        System.setProperty("HADOOP_USER_NAME", "root");
        org.apache.hadoop.conf.Configuration config = HBaseConfiguration.create();
        config.set("hbase.zookeeper.quorum", ZKPath);
        return ConnectionFactory.createConnection(config);
    }

    /**
     * 创建表
     *
     * @param conn
     * @param tableName
     * @throws IOException
     */
    public static void createTable(Connection conn, String tableName) throws Exception {
        Admin admin                         = conn.getAdmin();
        TableName tableNameClass            = TableName.valueOf(tableName);
        TableDescriptorBuilder tUserBuilder = TableDescriptorBuilder.newBuilder(tableNameClass);
        ColumnFamilyDescriptor tab          = ColumnFamilyDescriptorBuilder.of("tab");
        ColumnFamilyDescriptor video        = ColumnFamilyDescriptorBuilder.of("video");
        ColumnFamilyDescriptor user         = ColumnFamilyDescriptorBuilder.of("user");
        ColumnFamilyDescriptor userlog      = ColumnFamilyDescriptorBuilder.of("userlog");

        tUserBuilder.addColumnFamily(tab);
        tUserBuilder.addColumnFamily(video);
        tUserBuilder.addColumnFamily(user);
        tUserBuilder.addColumnFamily(userlog);

        TableDescriptor tableInfo = tUserBuilder.build();
        admin.createTable(tableInfo); // 建好后 list 查看
        admin.close();
        conn.close();
    }

    /**
     * 查询数据
     *
     * @param conn
     * @param tableName
     * @param rowkey
     * @throws IOException
     */
    public static void getByRowKey(Connection conn, String tableName, String rowkey) throws Exception {
        Table tableClass = conn.getTable(TableName.valueOf(tableName));
        Get getRK001     = new Get(Bytes.toBytes(rowkey));
        Result result    = tableClass.get(getRK001);
        List<Cell> cells = result.listCells();
        for (Cell cell : cells) {
            System.out.println("rowkey:" + Bytes.toString(CellUtil.cloneRow(cell)));
            System.out.println("columnFamily:" + Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println("Qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell)));
            System.out.println("--------------------------------------------------------------------------------");
        }
        tableClass.close();
        conn.close();
    }

    /**
     * 获取表中所有数据
     *
     * @param conn
     * @param tableName
     * @throws IOException
     */
    public static void getAll(Connection conn, String tableName) throws Exception {
        Table tableClass          = conn.getTable(TableName.valueOf(tableName));
        Scan scan                 = new Scan();
        ResultScanner scanner     = tableClass.getScanner(scan);
        Iterator<Result> iterator = scanner.iterator();
        while (iterator.hasNext()) {
            Result result    = iterator.next();
            List<Cell> cells = result.listCells();
            for (Cell cell : cells) {
                System.out.println("rowkey:" + Bytes.toString(CellUtil.cloneRow(cell)));
                System.out.println("columnFamily:" + Bytes.toString(CellUtil.cloneFamily(cell)));
                System.out.println("Qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
                System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell)));
                System.out.println("--------------------------------------------------------------------------------");
            }
        }
        tableClass.close();
        conn.close();
    }

    /**
     * 删除表
     *
     * @param conn
     * @param tableName
     * @throws IOException
     */
    public static void dropTable(Connection conn, String tableName) throws Exception {
        Admin admin = conn.getAdmin();
        admin.disableTable(TableName.valueOf(tableName));
        admin.deleteTable(TableName.valueOf(tableName));
        admin.close();
        conn.close();
    }
}
